package org.mall.modules.statistics.controller.seller;

import lombok.extern.slf4j.Slf4j;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.api.vo.Result;
import org.mall.common.constant.SellerCashStatusEnum;
import org.mall.common.system.util.JwtUtil;
import org.mall.common.util.DateUtil;
import org.mall.common.util.DateUtils;
import org.mall.common.util.json.JacksonUtils;
import org.mall.modules.cash.entity.SellerCashInfo;
import org.mall.modules.cash.service.ISellerCashInfoService;
import org.mall.modules.order.service.OrderCredentialService;
import org.mall.modules.order.service.OrderService;
import org.mall.modules.pojo.order.bo.OrderBO;
import org.mall.modules.pojo.order.dto.OrderCredentialDetailDTO;
import org.mall.modules.pojo.order.dto.OrderQueryDTO;
import org.mall.modules.pojo.shop.ShopDTO;
import org.mall.modules.pojo.shop.ShopDetailDTO;
import org.mall.modules.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/***
 *@author yinxucun
 *@date 2019-12-26 17:21 
 */
@RestController
@RequestMapping("/seller")
@Slf4j
public class SellerInfoController  {

   @Autowired
    private ShopService shopService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderCredentialService credentialService;
    @Autowired
    private ISellerCashInfoService sellerCashInfoService;



    @GetMapping("/getSellerPersonalFinanceInfo")
    public Result getSellerPersonalFinanceInfo(){

        List<ShopDetailDTO> list=shopService.getShopLisBySellerId(JwtUtil.getUserIdByToken());
        log.info(JacksonUtils.toJSONString(list));
        Map<String,String> result=new HashMap<>();

        List<String> shopIds=list.stream().map(ShopDetailDTO::getId).collect(Collectors.toList());
        if (shopIds.isEmpty()){
            result.put("ableCash","0");
            result.put("orderOfDay","0");
            result.put("totalIncome", "0");
            return Result.ok(result);
        }
        OrderQueryDTO vo=new OrderQueryDTO();
        vo.setShopIds(shopIds);
        vo.setStartClosingTime(DateUtil.getDayBegin());
        vo.setEndClosingTime(DateUtil.getDayEnd());
        CommonResult<List<OrderBO>> orderResult=orderService.getOrderBOListByWhereCondition(vo);
        List<SellerCashInfo> list1=sellerCashInfoService.getSellerCashInfoLogListByShopIdsAndStatus(shopIds,SellerCashStatusEnum.WAIT_CASH.getValue());
        if (orderResult.isSuccess()){
            long a= (long) orderResult.getData().size();
            result.put("orderOfDay", String.valueOf(a));
            result.put("ableCash", String.valueOf(list1.stream().mapToInt(SellerCashInfo::getPresentPrice).sum()));
                log.info(JacksonUtils.toJSONString(orderResult.getData()));
             long b=orderResult.getData().stream().mapToLong(o->{return o.getIncomePrice()!=null?o.getPresentPrice():0;}).sum();
             result.put("incomeOfDay",String.valueOf(b));
        }

        return Result.ok(result);
    }
    @GetMapping("/getClerkPersonalFinanceInfo")
    public Result getClerkPersonalFinanceInfo(){
        Map<String,String> result=new HashMap<>();
        CommonResult<List<ShopDTO>> shopResult=shopService.queryClerkBelongShops(JwtUtil.getUserIdByToken());
        List<ShopDTO> list=shopResult.isSuccess()?shopResult.getData(): Collections.emptyList();
        if (list.isEmpty()){
            result.put("orderNum","0");
            result.put("incomeOfDay","0");

            return Result.ok(result);
        }
        List<Long> orderIds=new ArrayList<>();
        CommonResult<List<OrderCredentialDetailDTO>> crentResult=credentialService.getOrderCredentialListBySellerIdAndDateRange(JwtUtil.getUserIdByToken(),
                DateUtils.formatDate(DateUtil.getDayBegin(),"yyyy-MM-dd HH:mm:ss"),DateUtils.formatDate(DateUtil.getDayEnd(),"yyyy-MM-dd HH:mm:ss"));
        if (crentResult.isSuccess()){
             orderIds=crentResult.getData().stream().map(OrderCredentialDetailDTO::getOrderId).collect(Collectors.toList());
        }
        if (orderIds.isEmpty()){
            result.put("orderNum","0");
            result.put("incomeOfDay","0");
            return Result.ok(result);
        }

        List<String> shopIds=list.stream().map(ShopDTO::getId).collect(Collectors.toList());

        OrderQueryDTO vo=new OrderQueryDTO();
        vo.setShopIds(shopIds);
        vo.setStartClosingTime(DateUtil.getDayBegin());
        vo.setEndClosingTime(DateUtil.getDayEnd());
        CommonResult<List<OrderBO>> orderResult=orderService.getOrderBOListByWhereCondition(vo);
        if (orderResult.isSuccess()){
            List<Long> finalOrderIds = orderIds;
            long a=orderResult.getData().stream().filter(orderPayAllChartBO -> {return orderPayAllChartBO.getId()!=null&& finalOrderIds.stream().anyMatch(c->orderPayAllChartBO.getId().equals(c));}).count();
            result.put("orderNum",String.valueOf(a));

            long b=orderResult.getData().stream().filter(orderPayAllChartBO -> {return orderPayAllChartBO.getId()!=null&& finalOrderIds.stream().anyMatch(c->orderPayAllChartBO.getId().equals(c));}).
                    mapToInt(OrderBO::getIncomePrice).sum();
            result.put("incomeOfDay",String.valueOf(b));
        }

        return Result.ok(result);
    }

}
